package com.x.erp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.erp.dao.CarConfDao;
import com.x.erp.dao.CarConfInfoDao;
import com.x.erp.dao.CarDao;
import com.x.erp.dao.CarImageDao;
import com.x.erp.manage.CarManage;
import com.x.erp.model.*;
import com.x.erp.rpc.CarRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Service(interfaceName = "com.x.erp.rpc.CarRpcService", timeout = 60000)
public class CarService extends BaseService implements CarRpcService {

	@Autowired
	private CarDao carDao;//车辆DAO

	@Autowired
	private CarConfInfoDao carConfInfoDao;//车辆配置关系DAO

	@Autowired
	private CarConfDao carConfDao;//车辆配置DAO

	@Autowired
	private CarManage carManage;//车辆Manage

	@Autowired
	private CarImageDao carImageDao;//车辆图片DAO

	/**
	 * 插入车辆信息
	 * @param car
	 * @return
	 */
	@Override
	public Car insertCar(Car car) {
		try {
			return carManage.insertCar(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 修改车辆信息
	 * @param car
	 * @return
	 */
	@Override
	public int updateCarInfo(Car car) {
		try {
			return carManage.updateCarInfo(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 修改车辆状态
	 * @param car
	 * @return
	 */
	@Override
	public int updateCarState(Car car) {
		try {
			return carManage.updateCarState(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 修改车辆信息
	 * @param car
	 * @return
	 */
	@Override
	public int updateCar(Car car) {
		return this.update(car);
	}

	/**
	 * 修改车辆信息
	 * @param carWhere
	 */
	@Override
	public int updateCar(Car carSet, Car carWhere) {
		return this.update(carSet, carWhere);
	}

	/**
	 * 查询车辆列表
	 * @param car
	 * @return
	 */
	@Override
	public List<Car> selectCarList(Car car) {
		try {
			return carDao.selectCarList(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0004, e);
		}
	}

	/**
	 * 查询车辆总数
	 * @param car
	 * @return
	 */
	@Override
	public int selectCarNum(Car car) {
		try {
			return carDao.selectCarNum(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	@Override
	public Car selectCar(Car car) {
		try {
			return carDao.selectCar(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}
	
	@Override
	public int deleteCar(Car car) {
		try {
			return carManage.deleteCar(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆配置
	 * @param carConfInfo
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarConfInfo(CarConfInfo carConfInfo) {
		try {
			return carConfInfoDao.selectCarConfInfo(carConfInfo);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆配置
	 * @param carConf
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarConf(CarConf carConf) {
		try {
			return carConfDao.selectCarConf(carConf);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 根据车辆ID查询图片信息
	 * @param carImage
	 * @return
	 */
	@Override
	public List<CarImage> selectCarImageByCarId(CarImage carImage) {
		try {
			return carImageDao.selectCarImageByCarId(carImage);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询入库信息
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public Map<String, Object> selectCarStorageInfo(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarStorageInfo(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 市场入库信息
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectMarketCarStorageInfo(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectMarketCarStorageInfo(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆出库信息
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public Map<String, Object> selectCarOutStockInfo(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOutStockInfo(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆预订信息
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public Map<String, Object> selectCarOrderInfo(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOrderInfo(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询库存品牌排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectStorageBrandRank(Car car, Date startDate, Date endDate, List<Car> cars){
		try {
			return carDao.selectStorageBrandRank(car, startDate, endDate, cars);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询出库品牌排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOutStockBrandRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOutStockBrandRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询预订品牌排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOrderBrandRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOrderBrandRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询库存车系排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectStorageSeriesRank(Car car, Date startDate, Date endDate, List<Car> cars){
		try {
			return carDao.selectStorageSeriesRank(car, startDate, endDate, cars);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询出库车系排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOutStockSeriesRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOutStockSeriesRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询预订车系排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOrderSeriesRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOrderSeriesRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询库存车龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectStorageCarAgeRank(Car car, Date startDate, Date endDate, List<Car> cars){
		try {
			return carDao.selectStorageCarAgeRank(car, startDate, endDate, cars);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	@Override
	public List<Map<String,Object>> selectStoragePriceRank(Car car, Date startDate, Date endDate, List<Car> cars) {
		try {
			return carDao.selectStoragePriceRank(car, startDate, endDate, cars);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}


	/**
	 * 查询车辆雷达图信息
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @param cars
	 * @return
	 */
	@Override
	public Map<String,Object> selectCarRadarInfo(Car car, Date startDate, Date endDate, List<Car> cars) {
		try {
			return carDao.selectCarRadarInfo(car, startDate, endDate, cars);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询出库车龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOutStockCarAgeRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOutStockCarAgeRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询预订车龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOrderCarAgeRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOrderCarAgeRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询库存库龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectStorageStockAgeRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectStorageStockAgeRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询出库库龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOutStockStockAgeRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOutStockStockAgeRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询预订库龄排行
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectCarOrderStockAgeRank(Car car, Date startDate, Date endDate){
		try {
			return carDao.selectCarOrderStockAgeRank(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆库存列表
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Car> selectCarStorageList(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarStorageList(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆库存列表数量
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public int selectCarStorageNum(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarStorageNum(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆出库列表
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Car> selectCarOutStockList(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOutStockList(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆出库列表数量
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public int selectCarOutStockNum(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOutStockNum(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆预订列表
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Car> selectCarOrderList(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOrderList(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车辆预订列表数量
	 * @param car
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public int selectCarOrderNum(Car car, Date startDate, Date endDate) {
		try {
			return carDao.selectCarOrderNum(car, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询相似价格车辆
	 * @param car 车辆信息
	 * @return
	 */
	@Override
	public List<Car> selectSimilarPriceCar(Car car) {
		try {
			return carDao.selectSimilarPriceCar(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 绑定RFID
	 * @param car
	 * @param carCard
	 * @return
	 */
	@Override
	public int updateCarBindRfid(Car car, String carCard) {
		try {
			return carManage.updateCarBindRfid(car, carCard);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(), e);
		}
	}

	/**
	 * 修改车辆图片
	 * @param image
	 * @return
	 */
	@Override
	public int updateCarImage(CarImage image) {
		return this.update(image);
	}

	/**
	 * 查询验证车架号
	 * @param car
	 * @return
	 */
	@Override
	public List<Car> selectValiCarVin(Car car) {
		try {
			return carDao.selectValiCarVin(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}
	/**
	 * 通过车辆订阅列表查询对应的车辆信息列表
	 * 
	 * @param carSelectTemplateList 多条车辆订阅信息，values封装查询条件
	 * @return 
	 */
	@Override
	public List<Car> selectCarListByCarSelectTemplateList(Car car, List<CarSelectTemplate> carSelectTemplateList) {
		try {
			return carDao.selectCarListByCarSelectTemplateList(car, carSelectTemplateList);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 修改车辆抵押状态
	 * @param car
	 * @return
	 */
	@Override
	public int updateCarOfPledge(Car car) {
		try {
			return carManage.updateCarOfPledge(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
	}

	/**
	 * 查询所有车辆信息，用于导入索引库
	 * @param car
	 * @return
	 */
	@Override
	public List<ESCar> selectAllCar(Car car) {
		try {
			return carDao.selectAllCar(car);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}
}
